﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyWebUtils;
using TTQT;
using TTQT.TTQT_Code;
public partial class TimKiemGiaoDich : MasterControl
{
    private void visiblePanels(bool timKiem, bool ketQua)
    {
        pnTimKiem.Visible = timKiem;
        pnGridMaster.Visible = ketQua;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            txtTuNgay.Value = DateTime.Now.AddDays(-7).ToString("dd/MM/yyyy");
            txtDenNgay.Value = DateTime.Now.ToString("dd/MM/yyyy");
            visiblePanels(true, false);
            bindKhoXuatNhan();
        }
    }
    private void bindKhoXuatNhan()
    {
        try
        {
            dsKhoXuat.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            dsKhoXuat.SelectCommand = "MaKho_LayChoTimKiem";
            dsKhoXuat.SelectParameters.Clear();
            dsKhoXuat.SelectParameters.Add(new Parameter("MaKho"));
            dsKhoXuat.SelectParameters["MaKho"].DefaultValue = UserInfo.MaKho;
            lstKhoXuat.DataSourceID = "dsKhoXuat";
            lstKhoXuat.DataBind();
        }
        catch (Exception)
        {

        }
        try
        {
            dsKhoNhan.SelectCommandType = SqlDataSourceCommandType.StoredProcedure;
            dsKhoNhan.SelectCommand = "MaKho_LayChoTimKiem";
            dsKhoNhan.SelectParameters.Clear();
            dsKhoNhan.SelectParameters.Add(new Parameter("MaKho"));
            dsKhoNhan.SelectParameters["MaKho"].DefaultValue = UserInfo.MaKho;
            lstKhoNhan.DataSourceID = "dsKhoNhan";
            lstKhoNhan.DataBind();
        }
        catch (Exception)
        {

        }
    }
    protected void cmdTimKiem_Click(object sender, EventArgs e)
    {
        ViewState["Tim"] = "1";
        #region KiemLoi

        DateTime tuNgay; //= TFCCommon.ToDateTime(txtTuNgay.Text);
        DateTime denNgay; //=TFCCommon.ToDateTime(txtDenNgay.Text);
        try
        {
            tuNgay = Convert.ToDateTime(txtTuNgay.Value);
        }
        catch
        {
            MyWebUtil.alert(this.Page, "Tu ngay sai dinh dang");
            return;
        }
        if (tuNgay > DateTime.Now)
        {
            MyWebUtil.alert(this.Page, "Tu ngay khong duoc lon hon ngay hien tai");
            return;
        }

        try
        {
            denNgay = Convert.ToDateTime(txtDenNgay.Value);
        }
        catch
        {
            MyWebUtil.alert(this.Page, "Den ngay sai dinh dang");
            return;
        }
        if (tuNgay > denNgay)
        {
            MyWebUtil.alert(this.Page, "Tu ngay phai truoc Den ngay");
            return;
        }

        /* Xong kiem tra loi */

        #endregion

        string whereClause = "";
        string sql = "select m.MaHoaDon,convert(varchar(10),m.Ngayxuatthucte,103) Ngayxuatthucte,convert(varchar(10),m.NgayNhanthucte,103) NgayNhanthucte, m.KhoXuat,m.KhoNhan,m.NoiDung,m.UserXuat,m.TrangThai,cast(n.TrangThai as varchar(5))+' - '+n.TenTrangThai [TenTrangThai] from HoaDon_Master m inner join TrangThai n on m.Trangthai=n.TrangThai where NgayXuat between'" + TTQTCommon.convertDateString(txtTuNgay.Value) + " 00:00:01' and '" +
                           TTQTCommon.convertDateString(txtDenNgay.Value) + " 23:59:59'";
        if (lstLoaiGiaoDich2.Items.Count > 0)
        {
            whereClause += " and LoaiGiaoDich in " + TTQTCommon.getAllValueOfListBox(lstLoaiGiaoDich2, true);
        }
        if (lstKhoNhan2.Items.Count > 0)
        {
            whereClause += " and KhoNhan in " + TTQTCommon.getAllValueOfListBox(lstKhoNhan2, false);
        }
        if (lstKhoXuat2.Items.Count > 0)
        {
            whereClause += " and KhoXuat in " + TTQTCommon.getAllValueOfListBox(lstKhoXuat2, false);
        }
        if (txtNoiDung.Text.Trim().Length > 0)
        {
            whereClause += " and NoiDung like N'%" + txtNoiDung.Text + "%'";
        }
        //Neu la user chi nhanh vao thi loc theo chi nhanh
        if (UserInfo.MaKho != "000")
        {
            whereClause += " and (KhoNhan='" + UserInfo.MaKho + "' or KhoXuat='" + UserInfo.MaKho + "')";
        }
        sql = sql + whereClause;
        ds.SelectCommandType = SqlDataSourceCommandType.Text;
        ds.SelectCommand = sql;
        try
        {
            masterGrid.DataSourceID = "ds";
            masterGrid.DataBind();
        }
        catch (Exception)
        {
            masterGrid.CurrentPageIndex = 0;
            masterGrid.DataSourceID = "ds";
            masterGrid.DataBind();
        }

        if (masterGrid.Items.Count < 1)
        {
            MyWebUtil.alert(this.Page, "Khong tim thay ket qua nao thoa man");
            return;
        }
        else visiblePanels(false, true);
    }

    protected void masterGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemIndex < 0) return;
        e.Item.CssClass = "Status0" + e.Item.Cells[1].Text;
        Button cmdXem = (Button)e.Item.FindControl("cmdXem");
        string khoXuat = "", khoNhan = "", url = "";
        khoXuat = e.Item.Cells[1].Text.Trim();
        khoNhan = e.Item.Cells[2].Text.Trim();
        if (khoXuat.Equals(UserInfo.MaKho))
        {
            url = "openPageXemGiaoDich('GiaoDich/InPhieuXuatKho.aspx?MaHoaDon=" + e.Item.Cells[0].Text + "&khoXuat=" +
                  khoXuat + "&khoNhan=" + khoNhan + "'); return false;";
        }
        else if (khoNhan.Equals(UserInfo.MaKho))
        {
            url = "openPageXemGiaoDich('GiaoDich/InPhieuNhapKho.aspx?MaHoaDon=" + e.Item.Cells[0].Text + "&khoXuat=" +
                  khoXuat + "&khoNhan=" + khoNhan + "'); return false;";
        }
        else
        {
            url = "openPageXemGiaoDich('GiaoDich/InHoaDon.aspx?MaHoaDon=" + e.Item.Cells[0].Text + "&khoXuat=" +
                  khoXuat + "&khoNhan=" + khoNhan + "'); return false;";
        }
        cmdXem.Attributes.Add("onclick", url);
    }

    protected void masterGrid_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
    {
        if (ViewState["Tim"].Equals("1"))
            cmdTimKiem_Click(null, null);
        if (ViewState["Tim"].Equals("2"))
            btnTimTheoSeri_Click(null, null);
        masterGrid.CurrentPageIndex = e.NewPageIndex;
        masterGrid.DataBind();
    }

    protected void btnAddLoaiGiaoDich_Click(object sender, EventArgs e)
    {
        TTQTCommon.addSelectedItem(lstLoaiGiaoDich, lstLoaiGiaoDich2);
        lstLoaiGiaoDich2.DataBind();
    }

    protected void btnRemoveLoaiGiaoDich_Click(object sender, EventArgs e)
    {
        TTQTCommon.removeSelectedItems(lstLoaiGiaoDich2);
    }

    protected void btnAddKhoXuat_Click(object sender, EventArgs e)
    {
        TTQTCommon.addSelectedItem(lstKhoXuat, lstKhoXuat2);
        lstLoaiGiaoDich2.DataBind();
    }

    protected void btnRemoveKhoXuat_Click(object sender, EventArgs e)
    {
        TTQTCommon.removeSelectedItems(lstKhoXuat2);
    }

    protected void btnAddBrn_Click(object sender, EventArgs e)
    {
        TTQTCommon.addSelectedItem(lstKhoNhan, lstKhoNhan2);
        lstLoaiGiaoDich2.DataBind();
    }

    protected void btnRemoveBrn_Click(object sender, EventArgs e)
    {
        TTQTCommon.removeSelectedItems(lstKhoNhan2);
    }
    protected void btnTimLai_Click(object sender, EventArgs e)
    {
        visiblePanels(true, false);
    }
    protected void btnTimTheoSeri_Click(object sender, EventArgs e)
    {
        ViewState["Tim"] = "2";
        #region Kiem Loi
        long number;
        try
        {
            if (txtTuSo.Text.Trim().Length > 0)
                number = Convert.ToInt32(txtTuSo.Text);
        }
        catch
        {
            MyWebUtil.alert(this.Page, "Chi so dau phai la 1 so");
            return;
        }
        try
        {
            if (txtDenSo.Text.Trim().Length > 0)
                number = Convert.ToInt32(txtDenSo.Text);
        }
        catch
        {
            MyWebUtil.alert(this.Page, "Chi so cuoi phai la 1 so");
            return;
        }
        #endregion
        string whereClause = " where (m.khoxuat='" + UserInfo.MaKho + "' or m.KhoNhan='" + UserInfo.MaKho + "')";
        string sql = "select distinct m.MaHoaDon,convert(varchar(10),m.Ngayxuatthucte,103) Ngayxuatthucte,convert(varchar(10),m.NgayNhanthucte,103) NgayNhanthucte, m.KhoXuat,m.KhoNhan,m.NoiDung,m.UserXuat,m.TrangThai,cast(n.TrangThai as varchar(5))+' - '+n.TenTrangThai [TenTrangThai] from HoaDon_Master m inner join TrangThai n on m.Trangthai=n.TrangThai inner join hoadon_detail d on m.MaHoaDon=d.MaHoaDon";
        if (txtSeri.Text.Trim().Length > 0)
        {
            whereClause += " and d.Seri ='" + txtSeri.Text + "'";
        }
        if (txtTuSo.Text.Trim().Length > 0)
        {
            whereClause += " and d.ChiSoDau =" + txtTuSo.Text;
        }
        if (txtDenSo.Text.Trim().Length > 0)
        {
            whereClause += " and d.ChiSoCuoi=" + txtDenSo.Text;
        }
        sql = sql + whereClause;
        ds.SelectCommandType = SqlDataSourceCommandType.Text;
        ds.SelectCommand = sql;
        try
        {
            masterGrid.DataSourceID = "ds";
            masterGrid.DataBind();
        }
        catch (Exception)
        {
            masterGrid.CurrentPageIndex = 0;
            masterGrid.DataSourceID = "ds";
            masterGrid.DataBind();
        }

        if (masterGrid.Items.Count < 1)
        {
            MyWebUtil.alert(this.Page, "Khong tim thay ket qua nao thoa man");
            return;
        }
        else visiblePanels(false, true);
    }
}
